<?php

namespace App\Services\Permission;

use App\Dao\Permission\RoleDao;
use App\Extends\BaseService;
use Illuminate\Support\Facades\DB;

class RoleService extends BaseService
{
    public function __construct(RoleDao $dao)
    {
        parent::__construct($dao);
        $this->dao = $dao;
    }

    /**
     * 创建角色
     * @param array $data
     * @return mixed
     * @throws \Exception
     */
    public function create(array $data): mixed
    {
        return $this->transactional(function () use ($data) {
            if(isset($data['rules'])) {
                $data['rules'] = ensureArray($data['rules']);
            }
            return $this->dao->create($data);
        });
    }

    /**
     * 编辑角色
     * @param int $id
     * @param array $data
     * @return mixed
     */
    public function update(int $id, array $data): mixed
    {
        return $this->transactional(function () use ($id, $data) {
            if(isset($data['rules'])) {
                $data['rules'] = ensureArray($data['rules']);
            }
            return $this->dao->update($id, $data);
        });
    }
}
